var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buthidden: false,
    previewImgIdCard: true,
    previewImgDriving: true,
    ifUploadIdCardText: "请上传身份证人像面",
    ifUploadDringText: "请上传",
    identityStatus: 3,//3小回收员 4 大回收员
    ifShowidentityTruck: false,
    FileIDrivingPaths: "",
    FileIdCardPaths: "",
    tempFilePaths: "",
    buthidden: false,
    sourceType: ['album', 'camera'],
    ifshowIdCardInfo: false,
    ifshowDrivingInfo: false,
    cardVo: {},
    drivingVo: {},
    userVo: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    that.onMineInfo();
    try {
      var res = wx.getSystemInfoSync()
      var platform = res.platform
      if (platform == 'ios') {
        util.msg("警告", "IOS系统暂不支持拍照，请从相册选择照片")
        this.setData({
          sourceType: ['album']
        })
      }

    } catch (e) { }

  },

  onMineInfo: function () {
    var that = this;
    wx.request({
      method: "POST",
      url: 'https://www.zaishengwang.com/wx/toHomeUser.do',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      data: {
        id: app.globalData.userId,
      },
      success: function (res) {
        var contacts;
        that.setData({ userVo: res.data.userVo, identityStatus: res.data.userVo.wxType });

        if (res.data.userVo.auditStatus == 3) {

          if (res.data.userVo.wxType == 4) {
            that.setData({
              ifUploadDringText: "已上传",
              ifshowDrivingInfo: true,
              ifShowidentityTruck: true,
              previewImgDriving: false,
              FileIDrivingPaths: res.data.userVo.drivingUrl,
            });
          }
          that.setData({
            ifUploadIdCardText: "已上传",
            ifshowIdCardInfo: true,
            previewImgIdCard: false,
            FileIdCardPaths: res.data.userVo.cardUrl,
            buthidden: true,
            cardVo: res.data.userVo,
            drivingVo: res.data.userVo
          });

        } else {
          that.setData({ ifUploadIdCardText: "请上传身份证人像面", ifUploadDringText: "请上传行驶证正面", });
        }
        // if (res.data.userVo.cardName != "" && res.data.userVo.cardName != null && res.data.userVo.cardName != undefined){
        //   that.setData({ cardVo: res.data.userVo, drivingVo: res.data.userVo});
        // }

      },
      fail: function (error) {
        console.log(error);
      }
    })
  },

  //上传身份证
  chooseImageIdCard: function () {
    var that = this;
    var Type = that.data.sourceType
    if (that.data.userVo.auditStatus != 3) {
      wx.chooseImage({
        count: 1, // 默认9 
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: Type, // 可以指定来源是相册还是相机，默认二者都有 
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
          that.setData({
            FileIdCardPaths: res.tempFilePaths
          })
          //console.log(that.data.FileIdCardPaths);
          wx.uploadFile({
            url: 'https://www.zaishengwang.com/wx/uploadFile.do',
            filePath: that.data.FileIdCardPaths[0],//图片路径，如tempFileIdCardPaths[0]
            name: 'image',
            userId: app.globalData.userId,
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: {
              userId: app.globalData.userId //附加信息为用户ID
            },
            success: function (res) {

              var cardVoStr = JSON.parse(res.data);
              if (cardVoStr.msg == "2") {
                wx.showModal({
                  title: '照片不清晰，请重新上传',
                  confirmColor: '#56BDE0',
                  showCancel: false,
                })
              } else {
                that.setData({ previewImgIdCard: false, ifshowIdCardInfo: true, ifUploadIdCardText: "已上传", cardVo: cardVoStr.cardVo });
                wx.showToast({
                  title: '上传成功',
                })
              }
            }, fail: function (err) {
              console.log(err)
            }
          })
        }
      })
    }
  },
  //上传行驶证
  chooseImageDriving: function () {
    var that = this;
    var Type = that.data.sourceType;
    if (that.data.userVo.auditStatus != 3) {

      wx.chooseImage({
        count: 1, // 默认9 
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: Type, // 可以指定来源是相册还是相机，默认二者都有 
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片 
          that.setData({
            FileIDrivingPaths: res.tempFilePaths
          })

          //console.log(that.data.FileIDrivingPaths);
          //console.log(app.globalData.userId);
          wx.uploadFile({
            url: 'https://www.zaishengwang.com/wx/uploadDrivingFile.do',
            filePath: that.data.FileIDrivingPaths[0],//图片路径，如tempFileIdCardPaths[0]
            name: 'image',
            userId: app.globalData.userId,
            header: {
              "Content-Type": "multipart/form-data"
            },
            formData: {
              userId: app.globalData.userId //附加信息为用户ID
            },
            success: function (res) {
              console.log(res);
              var cardVoStr = JSON.parse(res.data);
              console.log("cardVoStr", JSON.parse(res.data));

              if (cardVoStr.msg == "2") {
                wx.showModal({
                  title: '照片不清晰，请重新上传',
                  confirmColor: '#56BDE0',
                  showCancel: false,
                })
              } else {
                that.setData({ previewImgDriving: false, ifshowDrivingInfo: true, ifUploadDringText: "已上传", drivingVo: cardVoStr.drivingVo });
                wx.showToast({
                  title: '上传成功',

                })
              }
            }, fail: function (err) {
              console.log(err)
            }
          })
        }
      })

    }
  },

  updateUserStatus: function () {
    var that = this;
    var identityStatus = that.data.identityStatus;
    //console.log(that.data.cardVo);
    //console.log(that.data.cardVo.cardNum);
    if (identityStatus == 3) {
      if (that.data.cardVo.cardNum == undefined) {
        wx.showModal({
          title: '请上传身份证照片',
          confirmColor: '#56BDE0',
          showCancel: false,
        })
        return false;
      } else {
        that.setData({ buthidden: true });
        wx.request({
          method: "POST",
          url: 'https://www.zaishengwang.com/wx/updateUserStatus.do',
          dataType: 'json',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            bId: app.globalData.userId,
            wxType: identityStatus
          },
          success: function (res) {
            var c = 3;
            if (res.data.msg == "1") {
              wx.showToast({
                title: '身份认证成功',
                icon: 'success',
                duration: 1500
              });
              var intervalId = setInterval(function () {
                c = c - 1;
                if (c == 0) {
                  clearInterval(intervalId);
                  wx.reLaunch({
                    url: 'mine',
                  })
                }
              }, 1000)
            } else {
              that.setData({ buthidden: false });
            }

          },
          fail: function (error) {
          }
        })

      }

    } else if (identityStatus == 4) {
      if (that.data.cardVo.cardNum == undefined) {
        wx.showModal({
          title: '请上传身份证照片',
          confirmColor: '#56BDE0',
          showCancel: false,
        })
        return false;
      } else if (that.data.drivingVo.plateNum == undefined) {
        wx.showModal({
          title: '请上传车辆行驶证',
          confirmColor: '#56BDE0',
          showCancel: false,
        })
        return false;
      } else {
        that.setData({ buthidden: true });
        wx.request({
          method: "POST",
          url: 'https://www.zaishengwang.com/wx/updateUserStatus.do',
          dataType: 'json',
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          data: {
            bId: app.globalData.userId,
            wxType: identityStatus
          },
          success: function (res) {
            if (res.data.msg == "1") {
              wx.showToast({
                title: '身份认证成功',
                icon: 'success',
                duration: 1500
              });
              var intervalId = setInterval(function () {
                c = c - 1;
                if (c == 0) {
                  clearInterval(intervalId);
                  wx.reLaunch({
                    url: 'mine',
                  })
                }
              }, 1000)
            } else {
              wx.showModal({
                title: '请与管理员联系',
                confirmColor: '#56BDE0',
                showCancel: false,
              })
              that.setData({ buthidden: false });
            }
          },
          fail: function (error) {
          }
        })
      }
    }
  },
  //身份选择事件
  identitySeller: function () {
    var that = this;
    if (that.data.userVo.auditStatus != 3) {
      that.setData({
        identityStatus: 3,
        ifShowidentityTruck: false
      })
    }
  },

  identityTruck: function () {
    var that = this;
    if (that.data.userVo.auditStatus != 3) {
      that.setData({
        identityStatus: 4,
        ifShowidentityTruck: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})